package fr.gdi.android.news;

import android.appwidget.AppWidgetManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import fr.gdi.android.news.utils.Constants.Actions;
import fr.gdi.android.news.utils.Constants.URI;

public class WidgetUpdater extends BroadcastReceiver
{
    
    @Override
    public void onReceive(Context context, Intent intent)
    {
        if (TextUtils.equals(Actions.ACTION_UPDATE, intent.getAction()))
        {
            FetchTask fetchTask = new FetchTask();
            final int appWidgetId = intent.getExtras().getInt(AppWidgetManager.EXTRA_APPWIDGET_ID);
            if (appWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID)
            {
                Uri uri = URI.CONTENT_URI_FEEDS.buildUpon().appendEncodedPath(Integer.toString(appWidgetId)).build();
                fetchTask.execute(context, null, uri);
            }
            else
            {
                Uri uri = URI.CONTENT_URI_WIDGET_FEEDS.buildUpon().appendEncodedPath(Integer.toString(appWidgetId)).build();
                fetchTask.execute(context, appWidgetId, uri);
            }
        }
    }
    
}
